package com.health.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CrossDomainFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //允许可以访问的域
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:63342");
        //允许的请求方式
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        //本次预检请求的有效期，单位为秒。
        response.setHeader("Access-Control-Max-Age", "3600");
        //请求头中可携带的参数
        response.setHeader("Access-Control-Allow-Headers", "*");
        //允许携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");

        //过滤器放行
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
